Repository navigation
Evaluate fixed-operand effects before hoisted emplacement construction - #675
Merged
nunoplopes merged 1 commit intoNov 21, 2026
Merged
Conversation
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since a495c8d ("don't require rules to worry about run-time borrow errors"),
GetMappedAsStringmay construct an emplaced element (init) before the rule body runs. The receiver and theemplace_hinthint (the operands before the forwarded pack) are still evaluated later, inside the body. In C++ they are evaluated before the element is constructed. So the translation can copy stale data in both models, and in the refcount model an aliased receiver can panic with "RefCell already mutably borrowed".Example (
tests/unit/emplace_receiver_order.cpp):select_rows(&rows, &calls)->emplace_back(rows[0].data(), rows[0].data() + 3), whereselect_rowssetsrows[0][2] = 9. Native output inserts 9. The translation inserts 3 in both models.When a rule always constructs
initand is called on a receiver,GetMappedAsStringnow marks the receiver and any other non-pack operands that have side effects for binding, before it checks whetherinitmust be constructed first. If construction is hoisted, those operands are evaluated once, in C++ order, before construction and before the container is borrowed mutably. Evaluation uses the existing binding code. If construction is not hoisted, the original analysis and output are unchanged. Binding only after the hoisting decision fixes the wrong values but not the aliased-receiver panic, because there the binding is what allows construction to be hoisted.HasSideEffectstreats library calls as effectful, so hints such asm.end()orm.find(k)are now also bound first. This keeps their meaning. It adds one iterator binding (and a no-op clone) to the refcount output, which is whytests/unit/out/refcount/map.rschanges, with renumbered temporaries.Tests:
tests/unit/emplace_receiver_order.cpp: wrong value, plus a control with a separately bound receiver.tests/unit/emplace_receiver_alias.cpp: refcount panic through a pointer alias of the receiver.tests/unit/emplace_hint_receiver_order.cpp: hint side effects with a selected receiver and with a plain map.All three pass after this change, in both models. On Linux with clang 23, Release lit reports 649 passed and 33 expected failures, with the two socket cases below excluded; focused Debug lit reports 16 passed and 2 expected failures. The changed C++ files pass clang-format.
tests/unit/socket_ip_addrlen.candtests/unit/socket_recvfrom_name.cencounter native socket assertion failures on both revisions and remain for target CI.Not changed: side effects inside the forwarded arguments, e.g.
p.emplace_back(v, mod(&v)), are still evaluated in a different order from C++.try_emplace, which constructs only if the key is missing, is not affected. Hint performance (#611) is separate.Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.